﻿(function () {
    'use strict';

    angular.module('topics.services', []);
    angular.module('topics.controllers', []);

    var app = angular.module('topics', [
        'ui.router',
        'topics.services',
        'topics.controllers',
        'radical.directives']);

    var configHandler = function ($stateProvider, $locationProvider, $logProvider) {
        $logProvider.debugEnabled(true);

        //var nestedValidationViews = {
        //    '': {
        //        templateUrl: '/app/forms/presentation/nestedValidationView.html',
        //        controller: 'nestedValidationController'
        //    }
        //};

        $stateProvider
            .state('forms', {
                abstract: true,
                url: '',
                //views: nestedValidationViews
            })
            .state('forms.validation', {
                url: '/validation',
                views: {
                    '@': {
                        templateUrl: '/app/forms/presentation/validationView.html',
                        controller: 'validationController'
                    }
                }
            })
            .state('forms.nestedValidation', {
                url: '/nestedValidation',
                views: {
                    '@': {
                        templateUrl: '/app/forms/presentation/nestedValidationView.html',
                        controller: 'nestedValidationController'
                    }
                }
            });

        $locationProvider.html5Mode(false).hashPrefix('!');

        console.log('configuration completed.');
    };
    app.config(['$stateProvider', '$locationProvider', '$logProvider', configHandler]);

    var runHandler = function ($log, $rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$log = $log;
        $rootScope.$stateParams = $stateParams;
        $rootScope.safeApply = function ($scope, fn) {
            var phase = $scope.$root.$$phase;
            if (phase == '$apply' || phase == '$digest') {
                if (fn) {
                    $scope.$eval(fn);
                }
            } else {
                if (fn) {
                    $scope.$apply(fn);
                } else {
                    $scope.$apply();
                }
            }
        };
    };
    app.run(['$log', '$rootScope', '$state', '$stateParams', runHandler]);
}());